<?php
	class Category_Product extends MY_Controller{	
		function Category_Product() {
			parent::__construct();
			@session_start();
			$this->mod_ad->is_logged_in();
			$_SESSION['redirect'] = $this->uri->uri_string();		
		}
		
		function index(){
			$data['vl_sort'] = "DESC";	
			$data['title'] = "Danh mục sản phẩm";
			$data['table'] = 'category_product';
			$data['controller'] = 'category_product';
			$data['content_admin'] = 'back_end/category_product/index';
			$data['sort'] = 'ASC';
			$this->db->order_by('parent_id','asc');
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
		
			$per_page = 20;
			$uri = 4;
			$data['admin_category_product'] = $this->mod_lib->load_all('category_product','',array(),$per_page,$this->uri->segment($uri),array("root_path"=>"asc"));																													
			$config['base_url']     =  site_url('back_end/admin_menu/index/');
			$config['total_rows']   =  $this->db->count_all('category_product');
			$config['per_page']     =  $per_page; 
			$config['uri_segment']  =  $uri; 
			
			$this->pagination->initialize($config);
			$this->load->view('back_end/template_backend',$data);
		} 
		
		function add(){
			$data['title']			= "Thêm danh mục sản phẩm";
			$data['content_admin'] = 'back_end/category_product/add';
			$data['list_menu'] = $this->mod_lib->findOrderListCategory_Prd('category_product');
			$data['table'] = 'category_product';
			$data['properties'] = $this->db->get('properties')->result_array();
			$this->db->order_by('parent_id','asc');
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			$add = $this->input->post('addform');
			
			$data['properties'] = $this->db->get('properties')->result_array();	
			
			if(!empty($add)){ 	
				$data['admin_menu_value'] = $this->db->get('admin_menu')->result_array();
				$data['content_admin'] = 'back_end/category_product/index';
								
				if( $this->input->post('parent_id') == 0) {
					$arr_pr  = array();
				} else {
					$this->db->select('root_path,id');
					$arr_menu = $this->db->get_where('category_product', array('id' => $this->input->post('parent_id')))->row_array();
					$arr_pr   = explode(".",$arr_menu['root_path']);
				}
				
				if(count($arr_pr) == 0) {
					$level = 1;
				} if (count($arr_pr) == 1){
					$level = 2;  	
				} else if (count($arr_pr) == 2) {
					$level = 3;
				} else if (count($arr_pr) == 3) {
					$level = 4;
				}
				
				$data_value = array (
							'name'       		=> trim($this->input->post('name')),
							'parent_id'	        => trim($this->input->post('parent_id')),
							'alias'      		=> trim($this->input->post('alias')),
							'status'     		=> trim($this->input->post('status')),
							'level'      		=> $level				
						);
						
				if($this->mod_lib->insert('category_product',$data_value)) {
					$id = $this->db->insert_id();
					
				  	if ($this->input->post('parent_id') > 0) {
						$data  = array(
							'root_path'  => $arr_menu['root_path'].'.'.$id,
							'parent_id'  => $this->input->post('parent_id')	
						);
				  	} else {
				  		$data  = array(
				  			'root_path' => $id,
				  			'parent_id' => $this->input->post('parent_id')	
				  		);
				  	}
				
					$this->mod_lib->update('category_product',$where,$order,$data);
					$this->session->set_flashdata('msview', 'Thêm danh mục sản phẩm thành công !');
					$this->mod_lib->insert_log('Add category_product',$_SESSION['admin_id']);
				} else {	
					$this->session->set_flashdata('msview', 'Thêm danh mục sản phẩm thất bại !');
					$this->load->view('back_end/template_backend',$data);
				}
							
				redirect('back_end/category_product');
							
			} else {
				$this->load->view('back_end/template_backend',$data);
			}
		}
		
		function edit($id = null){
			$data['title']			= "Sửa danh mục tin tức";
			$data['content_admin'] = 'back_end/category_product/edit';
			$data['list_menu'] =  $this->mod_lib->findOrderListCategory_Prd('category_product');
			$data['admin_category_product_value'] = $this->db->get_where('category_product',array( 'id' => $id ))->row_array();
			$edit = $this->input->post('editForm');
			$this->db->order_by('parent_id','asc');
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			
			if(!empty($edit)){
				if ($id == $this->input->post('parent_id')) {
					$this->session->set_flashdata('msview', 'Sửa thất bại !');
					redirect('back_end/category_product');
				}
					
				if( $this->input->post('parent_id') == 0) {
					$arr_pr  = array();
				} else {
					$this->db->select('root_path,id');
					$arr_menu = $this->db->get_where('category_product', array('id' => $this->input->post('parent_id')))->row_array();
					$arr_pr  = explode(".",$arr_menu['root_path']);
				}
				
				if(count($arr_pr) == 0) {
					$level = 1;
				} if (count($arr_pr) == 1){
					$level = 2;  	
				} else if (count($arr_pr) == 2) {
					$level = 3;
				} else if (count($arr_pr) == 3) {
					$level = 4;
				}
				
				$where = array(
			  			'id' => $id 
			  		);
			  	$order = array(
			  			'id' => 'DESC' 
			  		);
			  		
			  	if ($this->input->post('parent_id') > 0) {
					$data_value = array (
						'name'       => trim($this->input->post('name')),
						'status'     => trim($this->input->post('status')),
						'alias'      => trim($this->input->post('alias')),
						'level'      => $level,	
						'root_path'  => $arr_menu['root_path'].'.'.$id,
						'parent_id'  => $this->input->post('parent_id')
					);
			  	} else {
			  		$data_value = array (
						'name'       => trim($this->input->post('name')),
						'status'     => trim($this->input->post('status')),
			  			'alias'     => trim($this->input->post('alias')),
						'level'      => $level,	
						'parent_id'  =>  $this->input->post('parent_id'),
			  			'root_path' =>  $id
					);
			  	}
		
				if($this->mod_lib->update('category_product',$where,$order,$data_value)){
					$this->session->set_flashdata('msview', 'Sửa thành công !');
				} else {
					$this->session->set_flashdata('msview', 'Sửa thất bại !');
				}
				
				redirect('back_end/category_product');
			}else {		
				$this->load->view('back_end/template_backend',$data);		
			}
		}
		
		function sort($field,$sort = ''){
			$per_page = 20;
			$uri = 4;
			$data['title'] = "Category Product - Sort";
			$data['table'] = 'category_Product';
			$data['controller'] = 'category_product';
			$data['list_menu'] = $this->mod_lib->findOrderList($data['table']);
			$data['content_admin'] = 'back_end/category_product/index';
			$this->db->order_by('parent_id','asc');
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			
			$_SESSION['sort'] = $sort;
			$_SESSION['field'] = $field;
			
			if ($sort == 'DESC') {
				$data['sort'] =  'ASC';
			} else if($sort == 'ASC') {
				$data['sort'] =  'DESC';
			}
			
			$data_search = $this->mod_lib->search('category_product','',array(),array(),array("id"=>"desc"));
			$data['admin_category_product'] = $this->mod_lib->load_search('category_product','',array(),array(),$per_page,$this->uri->segment($uri),array($_SESSION['field']=>$_SESSION['sort']));		
			$config['base_url']     =  site_url('back_end/category_product/index');

			$config['total_rows']   =  count($data_search);
			$config['per_page']     =  $per_page;
			$config['uri_segment']  =  $uri; 
			$this->pagination->initialize($config);		
			$this->load->view('back_end/template_backend',$data);		
		}
		
		function search(){
		}
		
		function deleteAll() {
			$value_id_arr    = $this->input->post('value_id');
			
			foreach ( $value_id_arr  as $ctg) {
				//xoa cac thuoc tinh tuong ung voi danh muc san pham 
				$id_arr = array (
					'product_id' => $ctg
				);
				$this->db->delete('product_properties',$id_arr); 
				
				$id_category = array (
					'category_product_id' => $ctg
				);
				$this->db->delete('product',$id_category);	
			}
			
			if(!empty($value_id_arr)){
				if($this->mod_lib->deleteCategory('category_product',$value_id_arr)){
					$this->session->set_flashdata('msview', 'Xóa thành công !');
				} else {
					$this->session->set_flashdata('msview', 'Xóa thất bại !');
				}
				
				$this->session->set_flashdata('msview', 'Xóa thành công !');
			} else {
				$this->session->set_flashdata('msview', 'Xóa thất bại !');
			}
			
			redirect('back_end/category_product');
		}
		
		function delete($id) {
			if(!empty($id)){
				//xoa cac thuoc tinh tuong ung voi danh muc san pham 
				$id_arr = array (
					'product_id' => $id
				);
				$this->db->delete('product_properties',$id_arr); 
				
				$id_category = array (
					'category_product_id' => $id
				);
				$this->db->delete('product',$id_category);
				
				if($this->mod_lib->deleteCategory('category_product',$id)){
					$this->session->set_flashdata('msview', 'Xóa thành công !');
				} else {
					$this->session->set_flashdata('msview', 'Xóa thất bại !');
				}
				
				$this->session->set_flashdata('msview', 'Xóa thành công !');
			} else {
				$this->session->set_flashdata('msview', 'Xóa thất bại !');
			}		
			redirect('back_end/category_product');
		}

		/**
		 * @desc: Cac thuoc tinh tuong ung voi danh muc
		 */
		function per($id){
			$data['title'] 			= " Phân quyền cho thành viên quản trị";
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			$data['ct_id'] = $id;
			$data['properties'] = $this->db->get('properties')->result_array();
			
			$this->db->select('name');
			$data['category'] = $this->db->get_where('category_product', array('id' => $id))->row_array();
			
			$data['content_admin']	= 'back_end/category_product/per';
			
			$sql =  "SELECT product_properties.properties_id, category_product.id as ctpd_id, properties.name, product_properties.kh  
					 FROM   category_product, product_properties , properties 
				 	 WHERE  category_product.id = $id
				 	 AND    category_product.id = product_properties.product_id
				 	 AND    properties.id = product_properties.properties_id  
				 	";
			
			$data['cate_pp'] = $this->db->query($sql)->result_array();
			
			foreach ($data['cate_pp'] as $cate_pp_arr) {
				$ct_p_value[] = $cate_pp_arr['ctpd_id'].'_'.$cate_pp_arr['properties_id'];
			}
			
			if($this->input->post('add')){	
				$category_properites = $this->input->post('ct_id');
				
				if(is_array($category_properites)) { 
					$arr_diff = array_diff($ct_p_value,$category_properites);
									
					foreach ($category_properites as $ct_p) {
						$ct_p_array = explode("_", $ct_p);		
						$where = array(
			  				'product_id' => $id ,
							'properties_id' => end($ct_p_array)
			  			);
				  		$order = array(
				  			'id' => 'DESC' 
				  		);	
						$data_value = array (
			  				'kh' =>  1
						);
						
						$sql_prd =  "SELECT category_product_pp.product_id, category_product_pp.properties_id
							 FROM   category_product_pp, product
							 WHERE  product.id = category_product_pp.product_id
							 AND    product.category_product_id = $id
							 
					 	";
						
						$prd_values = $this->db->query($sql_prd)->result_array();
						
						
						foreach ($prd_values as $prd_current) {
							if ($prd_current['properties_id'] !==  end($ct_p_array)) {
								$data_value_pp = array (
										'product_id'       		=> $prd_current['product_id'],
										'properties_id'	        => end($ct_p_array),
										'value'                 => ""			
								);
								
								$this->mod_lib->insert('category_product_pp',$data_value_pp);
							}
						}
						
						$this->mod_lib->update('product_properties',$where,$order,$data_value);
					}
	
					foreach ($arr_diff as $arr_element) {
						$arr_vl = explode("_", $arr_element);
						$where = array(
			  				'product_id' => $id ,
							'properties_id' => end($arr_vl)
			  			);
				  		$order = array(
				  			'id' => 'DESC' 
				  		);	
						$data_value = array (
			  				'kh' =>  0
						);
						
						$id_arr = array (
								'properties_id' => end($arr_vl)
						);
						$this->db->delete('category_product_pp',$id_arr);
						
						$this->mod_lib->update('product_properties',$where,$order,$data_value);	
					}
				} else {
					foreach ($ct_p_value as $arr_element) {
						$arr_vl = explode("_", $arr_element);
						$where = array(
			  				'product_id' => $id ,
							'properties_id' => end($arr_vl)
			  			);
				  		$order = array(
				  			'id' => 'DESC' 
				  		);	
						$data_value = array (
			  				'kh' =>  0
						);
						
						$id_arr = array (
								'properties_id' => end($arr_vl)
						);
						$this->db->delete('category_product_pp',$id_arr);
						
						$this->mod_lib->update('product_properties',$where,$order,$data_value);	
					}
				}
				
				$this->session->set_flashdata('msview', 'Thay đổi các thuộc tính của các danh mục thành công!');
				redirect('back_end/category_product/per/'.$id);
			}

			$this->load->view('back_end/template_backend',$data);
		}
	}
?>